<template>
  <div class="app-container">
    <div style="text-align: center;margin-bottom: 30px">
      合同{{ createStatus ? '新增' : '更新' }}
    </div>
    <el-row :gutter="10">
      <el-form :model="contractForm" ref="contractForm" :rules="contractFormRules" label-width="100px">
        <el-col :xs="24" :sm="24" :md="24" :lg="18" :xl="18">
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="合同名称" prop="name">
              <el-input type="text" v-model="contractForm.name" placeholder="请输入合同名称"
                        clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8" >
            <el-form-item label="维保单位" prop="bEnterpriseId">
              <el-select v-model="contractForm.enterpriseId" filterable placeholder="请选择主体" style="width: 100%">
                <el-option v-for="(item,index) in enterpriseList" :key="index" :value="item.ucId"
                           :label="item.name" ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
            <el-form-item label="合同状态" prop="cycle">
              <el-select style="width: 100%" v-model="contractForm.status" ref="selectUpResId" placeholder="请选择父级资源">
                <el-option
                    v-for="dict in statusOptions"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="合同年月起止" prop="startDate">
              <el-date-picker
                  v-model="contractDate"
                  type="monthrange"
                  format="yyyy-MM"
                  value-format="yyyy-MM"
                  align="right"
                  unlink-panels
                  range-separator="至"
                  start-placeholder="开始月份"
                  end-placeholder="结束月份"
                  @change="handleChange"
                  :picker-options="pickerOptions">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-col>
      </el-form>
    </el-row>


    <div slot="footer" style="text-align: center" class="dialog-footer">
      <el-button @click="cancelData">取 消</el-button>
    </div>
  </div>
</template>

<script>
import {getData, addData, updateData} from "@/api/contract";
import {getToken} from "@/utils/auth";
import {getInEnterpriseDataList} from "@/api/enterpriseDominant";

export default {
  name: "dataEdit",
  components: {},
  data() {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '本月',
          onClick(picker) {
            picker.$emit('pick', [new Date(), new Date()]);
          }
        }, {
          text: '今年至今',
          onClick(picker) {
            const end = new Date();
            const start = new Date(new Date().getFullYear(), 0);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近六个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setMonth(start.getMonth() - 6);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      contractForm: {
        name: "",
        enterpriseId: "",
        enterpriseName: "",
        startDate: "",
        endDate: ""
      },
      contractFormRules: {
        address: [{required: true, message: '详细地址不能为空', trigger: 'blur'}]
      },
      addLoading: false,
      contractDate: [],
      statusOptions: [],
      enterpriseList: []
    }
  },
  computed: {
    createStatus() {
      return this.$route.params.id == '0'
    },
    headers() {
      return {
        'Authorization': getToken()
      }
    }
  },
  created() {
    this.getInEnterpriseDataList()
    this.getDicts("contract_status").then(response => {
      this.statusOptions = response.data.data;
    });
    if (!this.createStatus) {
      let ucId = this.$route.params.id
      this.getData(ucId)
    }
  },
  methods: {
    handleChange(value){
      this.contractForm.startDate = value[0]
      this.contractForm.endDate = value[1]
    },
    getData(val) {
      getData({id: val}).then(res => {
        this.contractForm = res.data.data
        this.contractDate.push(this.contractForm.startDate)
        this.contractDate.push(this.contractForm.endDate)
      }).catch(res => {

      })
    },
    getInEnterpriseDataList() {
      getInEnterpriseDataList({deleted: false, dominantType: 7}).then(res => {
        this.enterpriseList = res.data
      })
    },
    cancelData() {
      window.history.back()
    },
    createData() {
      this.addLoading = true
      this.$refs['contractForm'].validate(valid => {
        if (valid) {
          console.log(this.contractForm)
          addData(this.contractForm).then(res => {
            this.addLoading = false
            this.$notify.success({
              title: '成功',
              message: "添加合同成功"
            })
            this.cancelData()
          }).catch(res => {
            this.addLoading = false
            this.$notify.error({
              title: '失败',
              message: res.msg
            })
          })
        } else {
          this.addLoading = false
          console.log("表单验证不通过")
        }
      })
    },
    updateData() {
      this.addLoading = true
      this.$refs['contractForm'].validate(valid => {
        if (valid) {
          updateData(this.contractForm).then(res => {
            this.addLoading = false
            this.$notify.success({
              title: '成功',
              message: "更新合同成功"
            })
            this.cancelData()
          }).catch(res => {
            this.addLoading = false
            this.$notify.error({
              title: '失败',
              message: res.msg
            })
          })
        } else {
          this.addLoading = false
          console.log("表单验证不通过")
        }
      })
    },
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 149px;
  height: 149px;
  line-height: 149px;
  text-align: center;
}

.avatar {
  width: 149px;
  height: 149px;
  display: block;
}
</style>
